package cn.uniquesoft.controller;

import cn.uniquesoft.manager.Client;
import cn.uniquesoft.manager.ClientManager;
import cn.uniquesoft.service.ILogService;
import cn.uniquesoft.service.IRoleService;
import cn.uniquesoft.service.IUserService;
import cn.uniquesoft.util.BrowserUtils;
import cn.uniquesoft.util.ContextUtil;
import cn.uniquesoft.util.IpUtil;
import cn.uniquesoft.util.Md5Util;
import cn.uniquesoft.vo.AnswerVO;
import cn.uniquesoft.vo.LogVO;
import cn.uniquesoft.vo.RoleVO;
import cn.uniquesoft.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author zyx
 * @date 2017年6月20日 下午6:15:22
 * @description 账号登录处理器
 */
@Controller
@RequestMapping(value = "/login")
public class LoginController {
    @Autowired
    private ILogService logSerivce;
    @Autowired
    private IUserService userService;
    @Autowired
    private IRoleService roleService;

    @RequestMapping(value = "/index")
    public String index() {

        return "login/index";
    }

    @RequestMapping(value = "/checkLogin")
    @ResponseBody
    public AnswerVO checkLogin(String cname, String cpassword, HttpServletRequest req) {
        AnswerVO answerVO = new AnswerVO();
        cpassword = Md5Util.md5(cpassword);
        UserVO userVO = this.userService.getUserByName(cname);
        if (userVO == null) {
            answerVO.setMsg("账号不存在");
            answerVO.setSuccess(false);
            return answerVO;
        }
        if (userVO.getIspassed() == 2) {
            answerVO.setMsg("账号未审核");
            answerVO.setSuccess(false);
            return answerVO;
        }
        if (!userVO.getCpassword().equals(Md5Util.md5(cpassword + userVO.getCsalt()))) {
            answerVO.setMsg("密码错误");
            answerVO.setSuccess(false);
            return answerVO;
        }

        answerVO.setSuccess(true);
        answerVO.setMsg("用户登陆验证成功！");
        RoleVO r = this.roleService.findOneById(userVO.getRid());
        userVO.setIvisible(r.getIvisible());
        addSession(userVO, req);
        addlog(userVO, req);
        return answerVO;
    }

    /**
     * 添加登录日志
     */
    private void addlog(UserVO userVO, HttpServletRequest req) {
        Date nowdate = new Date();
        try {
            LogVO logVO = new LogVO();
            logVO.setIlogid(userVO.getIid());
            logVO.setClogname(userVO.getCname());
            logVO.setClogtype("管理员登录");
            logVO.setCbrowser(BrowserUtils.checkBrowse(req));
            logVO.setCip(IpUtil.getIpAddr(req));
            logVO.setDlogdate(nowdate);
            this.logSerivce.save(logVO);
        } catch (Exception e) {
            System.out.println("日志记录异常！");
            e.printStackTrace();
        }
    }

    /**
     * 添加用户到session中,统计在线用户
     */
    private void addSession(UserVO userVO, HttpServletRequest req) {
        /** 统计在线用户 */
        try {
            /** 添加user信息到session中 */
            HttpSession session = ContextUtil.getSession();
            Client client = new Client();
            client.setAdddate(new Date());
            client.setCip(IpUtil.getIpAddr(req));
            client.setUser(userVO);
            client.setCbrowser("ht");
            ClientManager.getInstance().addClient(session.getId(), client);
            /** this.userLoginLogService.insertLog(); */
        } catch (Exception e) {
            System.out.println("用户在线人数统计，异常！");
            e.printStackTrace();
        }
    }

    /**
     * 退出系统
     */
    @RequestMapping(value = "userLoginOff")
    @ResponseBody
    public AnswerVO userLoginOff(UserVO item, HttpServletRequest req) {
        AnswerVO j = new AnswerVO();
        try {
            clearUserSession(req);
            j.setSuccess(true);
            j.setMsg("退出成功！");
        } catch (Exception e) {
            e.printStackTrace();
            j.setSuccess(false);
            j.setMsg("退出异常！");
        }
        return j;
    }

    /**
     * 退出时，清理用户session
     */
    private void clearUserSession(HttpServletRequest req) {
        /** 在在线用户列表中删除当前用户信息 */
        try {
            /** this.logService.updateExitLog(); */
            HttpSession session = ContextUtil.getSession();
            ClientManager.getInstance().removeClient(session.getId());
            session.invalidate();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
